﻿using System;
using System.Collections.Generic;
using System.Text;
using CSRedis;
using Newtonsoft.Json;

namespace ConsoleService.Cache
{
    public class CSRRedisCacheService : ICacheService
    {

        private CSRedisClient _redisCache = null;
        public CSRRedisCacheService(string connectionString)
        {
            //集群哨兵连接
            //var csredis = new CSRedisClient("myredismaster,password=Q1w2e3r4", new string[] { "59.110.163.255:26379" });4
            var csredis = new CSRedisClient(connectionString);
            RedisHelper.Initialization(csredis);
            _redisCache = csredis;
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T">泛型(返回的结果类型)</typeparam>
        /// <param name="key">缓存key</param>
        /// <returns></returns>
        public T Get<T>(string key) where T : new()
        {
            try
            {
                if (!string.IsNullOrEmpty(key))
                {
                    return _redisCache.Get<T>(key);
                }
                else
                {
                    return default(T);
                }
            }
            catch
            {
                return default(T);
            }
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <returns></returns>
        public string Get(string key)
        {
            try
            {
                if (!string.IsNullOrEmpty(key))
                {
                    return _redisCache.Get(key);
                }
                else
                {
                    return string.Empty;
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存值</param>
        /// <param name="ExpirationTime">绝对过期时间(分钟)</param>
        public void Add(string key, object value, int expirationTime = 20)
        {
            if (!string.IsNullOrEmpty(key))
            {
                _redisCache.Set(key, JsonConvert.SerializeObject(value), expirationTime * 60);
            }
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">缓存key</param>
        public void Remove(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                _redisCache.Del(key);
            }
        }
        /// <summary>
        /// 更新缓存
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存值</param>
        /// <param name="ExpirationTime"></param>
        public void Replace(string key, object value, int expirationTime = 20)
        {
            if (!string.IsNullOrEmpty(key))
            {
                _redisCache.Del(key);
                _redisCache.Set(key, JsonConvert.SerializeObject(value), expirationTime * 60);
            }
        }

        public T GetOrSet<T>(string key, Func<T> acquire, int expirationTime = 20)
        {
            if (Exists(key))
            {
                return _redisCache.Get<T>(key);
            }
            else
            {
                var result = acquire();
                if (result != null)
                    Add(key, result, expirationTime);
                return result;
            }
        }

        public CSRedisClient GetRedis()
        {
            return _redisCache;
        }


        private bool Exists(string Key)
        {
            return _redisCache.Exists(Key);
        }

        #region 使用实例
        //private void test()
        //{
        //    ICacheService CacheService = ServiceHelper.GetService<ICacheService>();
        //    if (CacheService != null)
        //    {
        //        var model = _sysUser.GetUser("");
        //        CacheService.Add("SysUser", model);
        //        var json = CacheService.Get<SysUser>("SysUser");
        //    }
        //}
        #endregion
    }
}
